package org.artifact.xls;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import org.artifact.xls.convert.type.AbstractTypeConvert;
import org.artifact.xls.convert.type.CsTypeConvert;
import org.artifact.xls.convert.type.JavaTypeConvert;

@Getter
public class ExcelFieldType {
    private String name;
    private String[] generics = {};

    public AbstractTypeConvert getTypeConvert(String tag) {
        switch (tag) {
            case "cs":
                return new CsTypeConvert();
            case "java":
                return new JavaTypeConvert();
        }
        return null;
    }

    public ExcelFieldType(String type) {
        this.name = type;
//        this.name = StrUtil.subBefore(type, "<", false);
//        // 处理泛型
//        if (StrUtil.containsAny(type, "<", "|", ">")) {
//            String generic = StrUtil.subBetween(type, "<", ">");
//            String[] generics = StrUtil.split(generic, "|");
//            this.generics = new String[generics.length];
//            for (int i = 0; i < generics.length; i++) {
//                this.generics[i] = generics[i];
//            }
//        }
    }

    public String getBaseName(){
        // 取得名称
        String name = StrUtil.subBefore(this.name, "<", false);
        return name;
    }

    public String getIntact(String tag){
        return getIntact(tag,this.name,true);
    }

    // 取得完整类型
    public String getIntact(String tag,String name,boolean baseType) {

        // 取得泛型
        int fromIndex = name.indexOf("<");
        int toIndex = name.lastIndexOf(">");
        String generic = StrUtil.sub(name, fromIndex!=-1?fromIndex+1:-1, toIndex);

        // 取得名称
        String _name = StrUtil.subBefore(name, "<", false);
        _name = getTypeConvert(tag).convert(_name,baseType);

        if (!StrUtil.isEmpty(generic)){
            int odd = generic.indexOf("<");
            int even = generic.indexOf("|");
            if (odd > 0 && even > 0 && odd < even){
                _name += ("<" + getIntact(tag,generic,false) + ">");
            }else{
                String[] generics = StrUtil.splitToArray(generic, '|',2);
                for (int i = 0; i < generics.length; i++) {
                    generics[i] = getIntact(tag,generics[i],false);
                }
                if(!ArrayUtil.isEmpty(generics)){
                    _name+= ("<" + ArrayUtil.join(generics,",") + ">");
                }
            }
        }
        return _name;
    }


    public String getGeneric(String tag){
        String intact = getIntact(tag,this.name,false);
        int fromIndex = intact.indexOf("<");
        int toIndex = intact.lastIndexOf(">");
        String generic = StrUtil.sub(intact, fromIndex!=-1?fromIndex+1:-1, toIndex);
        return generic;
    }

    public static void main(String[] args) {
        ExcelFieldType type = new ExcelFieldType("map<int|map<int|int>>");
        type = new ExcelFieldType("list<map<int|int>>");
        type = new ExcelFieldType("map<int|int>");
        type = new ExcelFieldType("list<int>");
        System.out.println(type.getIntact("java"));
    }
}
